package com.kygeng.datamock.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kygeng.common.model.database.CompanyInfo;
import lombok.Data;

import java.io.File;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * @Title: MockCompanyInfoUtil
 * @Author: kygeng
 * @Package: com.kygeng.dataupdate.util
 * @Date: 2024/8/31 10:54
 * @Description: 模拟生成公司数据
 */
public class MockCompanyInfoUtil {

    private final static String[] companyAddress = {"北京", "上海", "深圳", "广州", "杭州", "南京", "武汉", "长沙", "西安", "成都", "重庆", "天津", "苏州", "无锡", "济南", "青岛", "郑州", "合肥", "福州", "厦门", "南昌", "贵阳", "昆明", "南宁", "拉萨", "海口", "兰州", "西宁", "银川"};

    private final static String[] companyScale = {"大型企业", "中型企业", "小型企业", "微型企业", "小微企业"};

    private final static String[] companyType = {"有限责任公司", "股份有限公司", "集体所有制", "外商投资企业", "港澳台投资企业", "个人独资企业", "联营企业", "有限合伙"};

    private final static String[] companyStatus = {"存续", "迁出", "注销", "吊销，未注销", "停业", "撤销", "非正常户", "吊销，已注销", "责令关闭"};

    //
    private final static String[] companyRoad = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    //币种
    private final static String[] companyCurrency = {"USD", "EUR", "JPY", "CNY", "GBP", "AUD"};

    // 常见的中文姓氏
    private static final String[] SURNAME = {
            "赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",
            "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章",
    };

    //常用汉字数组
    private static final String[] CHARACTERS = {
            "一", "二", "三", "四", "五", "六", "七", "八", "九", "十",
            "百", "千", "万", "亿", "兆", "龙", "凤", "虎", "豹", "狼",
            "狮", "熊", "鹰", "鹏", "鹤", "鹿", "马", "牛", "羊", "犬",
            "猫", "狗", "鸟", "鱼", "兔子", "乌龟", "仓鼠", "鹦鹉", "金鱼", "蝴蝶",
            "蜜蜂", "蚂蚁", "蜘蛛", "蜻蜓", "蝉", "萤火虫", "蝴蝶兰", "牡丹", "玫瑰", "菊花",
            "荷花", "梅花", "兰花", "竹子", "松树", "柳树", "枫树", "梧桐", "樱花", "桃花",
            "梨花", "杏花", "茉莉", "牡丹", "郁金香", "百合", "康乃馨", "向日葵", "紫罗兰", "牵牛花",
            "果", "菜", "稻", "麦", "豆", "米", "谷", "茶", "果", "蔬",
            "风", "云", "雷", "电", "雨", "雪", "霜", "露", "冰", "水",
            "宇", "宙", "时", "间", "秒", "分", "钟", "时", "日", "周",
            "月", "季", "年", "春", "夏", "秋", "冬", "晨", "午", "晚",
            "和平", "发展", "合作", "交流", "学习", "工作", "生活", "家庭", "健康", "快乐",
            "成功", "进步", "创新", "友谊", "幸福", "安全", "经济", "社会", "文化", "艺术",
            "科学", "技术", "教育", "环境", "生态", "自然", "历史", "未来", "梦想", "希望",
            "理想", "目标", "挑战", "机遇", "责任", "义务", "权利", "自由", "平等", "公正",
            "民主", "法治", "文明", "和谐", "诚信", "爱国", "敬业", "友善", "勤劳", "勇敢",
            "智慧", "知识", "信息", "网络", "数据", "计算", "程序", "语言", "软件", "硬件",
            "电脑", "手机", "网络", "安全", "通信", "卫星", "电视", "广播", "电影", "音乐",
            "舞蹈", "绘画", "雕塑", "摄影", "书法", "戏曲", "小说", "诗歌", "散文", "报告"
    };

    // 行业词汇数组
    private static final String[] INDUSTRIES = {
            "科技", "网络", "信息", "电子", "生物", "医药", "化工", "机械", "贸易", "物流",
            "服务", "咨询", "文化", "传媒", "教育", "金融", "保险", "证券", "银行", "基金",
            "投资", "地产", "建筑", "装饰", "设计", "园林", "环保", "能源", "电力", "煤炭",
            "石油", "天然气", "太阳能", "风能", "核能", "新能源", "汽车", "航空", "航天", "船舶",
            "服装", "鞋子", "帽子", "手套", "眼镜", "手表", "首饰", "包包", "化妆品", "香水",
            "家具", "家电", "床铺", "沙发", "椅子", "桌子", "灯具", "地毯", "窗帘", "窗户",
            "服装", "鞋帽", "纺织", "皮革", "家具", "家电", "日化", "化妆品", "珠宝", "首饰",
            "钟表", "眼镜", "玩具", "体育", "健身", "器材", "图书", "出版", "印刷", "包装",
            "广告", "会展", "公关", "法律", "会计", "审计", "税务", "人力资源", "管理", "培训",
            "软件", "硬件", "互联网", "大数据", "云计算", "人工智能", "区块链", "物联网", "虚拟现实", "增强现实"
    };


    private static List<Map<String, String>> areaMap = new ArrayList<>();

    private static List<Map<String, String>> industryMap = new ArrayList<>();

    private static Integer companyCodeBase = 1000000000;

    static {
        String string = FileUtil.readString(new File("D:\\project\\myproject\\business-data-sync-model\\data-mock-service\\target\\classes\\standard_code.json"), "UTF-8");
        JSONArray jsonArray = JSONArray.parseArray(string);
        for (Object o : jsonArray) {
            JSONObject obj = (JSONObject) o;
            String type = obj.getString("type");
            if (StrUtil.isEmpty(type)) {
                continue;
            }
            if (type.equals("industry")) {
                industryMap.add(JSONObject.toJavaObject(obj, Map.class));
            }
            if (type.equals("area")) {
                areaMap.add(JSONObject.toJavaObject(obj, Map.class));
            }
        }
    }


    public static CompanyInfo getRandomCompanyInfo(Integer i) {

        String companyCode = (companyCodeBase + i) + "";
        String companyName = MockCompanyInfoUtil.getRandomCompanyName();
        String social_credit_code = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
        String legal_representative = MockCompanyInfoUtil.generateSingleName();
        Integer capital_registered = new Random().nextInt(8000);//留小数点后两位
        Integer capital_paid = new Random().nextInt(5000);//
        String currency = MockCompanyInfoUtil.getRandomCurrency();
        String enterprise_scale = MockCompanyInfoUtil.getRandomCompanyScale();//企业规模
        String randomCompanyType = MockCompanyInfoUtil.getRandomCompanyType();//企业类型
        String randomRegistrationStatus = MockCompanyInfoUtil.getRandomRegistrationStatus();//登记状态
        String incorporation_date = MockCompanyInfoUtil.getRandomDate();
        String business_date_start = MockCompanyInfoUtil.getRandomDate();
        String business_date_end = getEndData(incorporation_date, 50);

        Map<String, String> address = MockCompanyInfoUtil.getRandomCompanyAddress();
        String areaCode = address.get("code");
        String address_register = address.get("name") + MockCompanyInfoUtil.getNum();
        String address_office = address.get("name") + MockCompanyInfoUtil.getNum();

        LatitudeAndLongitude randomLatLon = getRandomLatitudeAndLongitude();

        String industry_code = MockCompanyInfoUtil.getIndustry();

        CompanyInfo companyInfo = new CompanyInfo();
        companyInfo.setId(i);
        companyInfo.setCompanyCode(companyCode);
        companyInfo.setCompanyName(companyName);
        companyInfo.setSocialCreditCode(social_credit_code);
        companyInfo.setLegalRepresentative(legal_representative);

        companyInfo.setCapitalRegistered(Double.valueOf(capital_registered));
        companyInfo.setCapitalPaid(Double.valueOf(capital_paid));
        companyInfo.setCapitalRegisteredCurrency(currency);
        companyInfo.setCapitalPaidCurrency(currency);

        companyInfo.setCompanyScale(enterprise_scale);
        companyInfo.setCompanyStatus(randomRegistrationStatus);
        companyInfo.setCompanyType(randomCompanyType);

        companyInfo.setEstablishmentData(incorporation_date);
        companyInfo.setBusinessDateStart(business_date_start);
        companyInfo.setBusinessDateEnd(business_date_end);
        companyInfo.setAddressLat(randomLatLon.getLat());
        companyInfo.setAddressLon(randomLatLon.getLon());

        companyInfo.setAddressRegister(address_register);
        companyInfo.setAddressOffice(address_office);
        companyInfo.setAreaCode(areaCode);
        companyInfo.setIndustryCode(industry_code);

        return companyInfo;
    }

    public static LatitudeAndLongitude getRandomLatitudeAndLongitude() {

        int lonBase = 120;
        int latBase = 30;

        double lonV = lonBase + Math.random() * 2;
        double latV = latBase + Math.random() * 2;

        String lon = String.valueOf(lonV);
        String lat = String.valueOf(latV);

        LatitudeAndLongitude res = new LatitudeAndLongitude();
        res.setLat(lat);
        res.setLon(lon);
        return res;
    }

    @Data
    private static class LatitudeAndLongitude {
        private String lat;
        private String lon;
    }


    public static String getRandomCompanyName() {

        Random random = new Random();

        // 随机选择两个字符和一个行业词汇组成公司名
        String character1 = CHARACTERS[random.nextInt(CHARACTERS.length)];
        String character2 = CHARACTERS[random.nextInt(CHARACTERS.length)];
        String character3 = CHARACTERS[random.nextInt(CHARACTERS.length)];
        String industry = INDUSTRIES[random.nextInt(INDUSTRIES.length)];

        return character1 + character2 + character3 + industry + "有限公司";
    }

    private static String generateSingleName() {

        Random random = new Random();

        // 随机选择一个姓氏
        String surname = SURNAME[random.nextInt(SURNAME.length)];
        // 随机选择一个或两个名字用字
        if (random.nextBoolean()) {
            // 选择一个名字用字
            return surname + CHARACTERS[random.nextInt(CHARACTERS.length)];
        } else {
            // 选择两个名字用字
            String firstCharacter = CHARACTERS[random.nextInt(CHARACTERS.length)];
            String secondCharacter = CHARACTERS[random.nextInt(CHARACTERS.length)];
            return surname + firstCharacter + secondCharacter;
        }
    }

    public static String getRandomCompanyScale() {
        Random random = new Random();
        int i = random.nextInt(companyScale.length);
        return companyScale[i];
    }

    public static String getRandomCurrency() {
        Random random = new Random();
        int i = random.nextInt(companyCurrency.length);
        return companyCurrency[i];
    }


    public static String getRandomRegistrationStatus() {
        Random random = new Random();
        int i = random.nextInt(companyStatus.length);
        return companyStatus[i];
    }

    public static String getRandomCompanyType() {
        Random random = new Random();
        int i = random.nextInt(companyType.length);
        return companyType[i];
    }

    public static String getRandomDate() {
        // 设置日期范围
        LocalDate startDate = LocalDate.of(2000, 1, 1);
        LocalDate endDate = LocalDate.of(2024, 8, 30);

        // 生成随机日期
        long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
        Random random = new Random();
        long randomDays = random.nextInt((int) daysBetween + 1);
        LocalDate randomDate = startDate.plusDays(randomDays);

        // 格式化日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        return randomDate.format(formatter);
    }


    public static Map<String, String> getRandomCompanyAddress() {
        Random random = new Random();
        int i = random.nextInt(areaMap.size());
        return areaMap.get(i);
    }

    public static String getIndustry() {
        Random random = new Random();
        int i = random.nextInt(industryMap.size());
        Map<String, String> stringStringMap = industryMap.get(i);
        return stringStringMap.get("code");
    }

    public static String getRandomCompanyRoad() {
        Random random = new Random();
        int i = random.nextInt(companyRoad.length);
        return companyRoad[i];
    }

    public static String getNum() {
        Random random = new Random();
        return "," + getRandomCompanyRoad() + "路," + random.nextInt(5000) + "号";
    }

    public static String getEndData(String dateStr, int years) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate currentDate = LocalDate.parse(dateStr, formatter);
        LocalDate futureDate = currentDate.plusYears(years);
        String formattedDate = futureDate.format(formatter);
        return formattedDate;
    }

}
